use std::thread::sleep;
use std::time::Duration;
use anyhow::Error;
use server::utils::jwt::JwtToken;

#[test]
fn test_jwt() {
    let token = JwtToken::new("test".to_string());
    let token_str = token.create_token().unwrap();
    println!("{}", token_str);
    let decoded_token = JwtToken::decode_token(&token_str).unwrap();
    println!("{:?}", decoded_token);

    let token = JwtToken::new_with_timeout("test".to_string(), 1);
    let token_str = token.create_token().unwrap();
    println!("{}", token_str);
    println!("{}", chrono::Local::now().timestamp());
    sleep(Duration::from_secs(10));
    println!("{}", chrono::Local::now().timestamp());
    
    let result = JwtToken::decode_token(&token_str);
    match result {
        Ok(_) => panic!("Token should have expired!"),
        Err(e) => {
            println!("Token correctly expired with error: {:?}", e);
            assert!(e.to_string().contains("ExpiredSignature") || e.to_string().contains("expired"));
        }
    }
}
